package com.sdkj.recruit.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdkj.recruit.dao.IMerchantManagementDao;
import com.sdkj.recruit.dao.ISmallNoManagementDao;
import com.sdkj.recruit.model.Merchant;
import com.sdkj.recruit.model.SmallNo;
import com.sdkj.recruit.model.SmallNoLog;
import com.sdkj.recruit.model.SmallNoTask;
import com.sdkj.recruit.service.ISmallNoManagementService;
import com.sdkj.recruit.util.RandomUtil;
import com.sdkj.recruit.vo.SmallNoLogVo;
import com.sdkj.recruit.vo.SmallNoStatisticVo;
import com.sdkj.recruit.vo.SmallNoTaskVo;
import com.sdkj.recruit.vo.SmallNoVo;


@Service
public class SmallNoManagementService implements ISmallNoManagementService {

	@Autowired
	private ISmallNoManagementDao samllNoDao;
	
	@Autowired
	private IMerchantManagementDao merchantDao;
	
	@Autowired
	private Mapper mapper;

	@Override
	public List<SmallNoStatisticVo> querySmallNoList(int currentPage,
			int pageSize, String smallNoName, String platformName) {
		
		List<SmallNoStatisticVo> smallNoVoList = new ArrayList<SmallNoStatisticVo>();
		List<SmallNo> list =samllNoDao.querySmallNoList(currentPage,pageSize,smallNoName, platformName);
		
		if(CollectionUtils.isNotEmpty(list)){
			
			for(SmallNo samll:list){
				
				SmallNoStatisticVo vo = new SmallNoStatisticVo();
				vo.setCreateDate(samll.getCreateDate());
				vo.setMerchantId(samll.getMerchantId());
				//小号任务数量
				long taskCont =samllNoDao.queryTaskById(samll.getMerchantId(),samll.getSmallNoId());
				vo.setTaskCount(Integer.valueOf(String.valueOf(taskCont)));
				Merchant merchant=  merchantDao.queryMerchantById(samll.getMerchantId());
				if(merchant!=null){
					vo.setMerchantName(merchant.getMerchantName());
				}
				vo.setPlatformType(samll.getPlatformType());
				vo.setPlatformName(samll.getPlatformName());
				vo.setSmallNoName(samll.getSmallNoName());
				smallNoVoList.add(vo);
			}
		}
		return smallNoVoList;
	}

	@Override
	public long queryMerchantCount(int currentPage, int pageSize,
			String smallNoName, String platformName) {
		
		return samllNoDao.queryMerchantCount(currentPage,pageSize,smallNoName, platformName);
	}

	@Override
	public boolean saveSmallNo(SmallNoVo smallNoVo) {
		
		String smallNoId =new RandomUtil().getRandomId();
		smallNoVo.setSmallNoId(smallNoId);
		smallNoVo.setCreateDate(new Date());
		SmallNo samllNo = mapper.map(smallNoVo, SmallNo.class);
		samllNoDao.saveSmallNo(samllNo);
		return true;
	}

	@Override
	public boolean saveSmallNoLog(SmallNoLogVo smallNoLogVo) {
		
		SmallNoLog log = mapper.map(smallNoLogVo, SmallNoLog.class);
		samllNoDao.saveSmallNoLog(log);
		return true;
	}

	@Override
	public boolean saveSmallNoTask(SmallNoTaskVo smallNoTaskVo) {
		
		String taskId =new RandomUtil().getRandomId();
		smallNoTaskVo.setTaskId(taskId);
		smallNoTaskVo.setCreateDate(new Date());
		SmallNoTask mTask = mapper.map(smallNoTaskVo, SmallNoTask.class);
		merchantDao.saveSmallNoTask(mTask);
		
		return true;
	}
	
}
